/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       drag_artifact.cpp

	$Header: /game/drag_artifact.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "drag_artifact.h"

#include "artifact_icon.h"
#include "artifact_properties.h"
#include "artifact_type.h"
#include "bitmap_layer.h"
#include "bitmap_layer_cache_window.h"
#include "drag_artifact_source_holder.h"

// -------------------------------------------------------------------
// window which holds an artifact which can be dragged away
// -------------------------------------------------------------------
t_drag_artifact_source::t_drag_artifact_source( t_screen_rect const& rect, 
											    t_artifact_slot slot, 
												t_drag_artifact_source_holder* holder,
												t_window* parent )
					  : t_window( rect, k_some_transparency, parent ), 
					    m_slot( slot ), m_holder( holder )
{
	m_icon = new t_artifact_icon( t_artifact(), t_screen_point(0,0), this );
}

void t_drag_artifact_source::set( t_artifact const& artifact, t_creature_stack* stack,
										 int backpack_slot )
{
	m_artifact = artifact;
	m_backpack_slot = backpack_slot;
	m_stack = stack;
	m_icon->set_artifact( artifact );
}

// -------------------------------------------------------------------
// window which holds an artifact which can be dragged away
// -------------------------------------------------------------------
bool t_drag_artifact_source::accept_drag( t_drag_object* drag, t_mouse_event const& event)
{
	t_counted_ptr<t_drag_artifact> drag_artifact = dynamic_cast<t_drag_artifact*>(drag);

	if (drag_artifact != 0)
	{
		m_holder->accept_artifact_drag( drag_artifact, this );
		return true;
	}
	return t_window::accept_drag( drag, event );
}

// -------------------------------------------------------------------
// window which holds an artifact which can be dragged away
// -------------------------------------------------------------------
void t_drag_artifact_source::restore( t_artifact const& artifact )
{
	m_artifact = artifact;
	m_holder->restore( this );
}

// -------------------------------------------------------------------
// window which holds an artifact which can be dragged away
// -------------------------------------------------------------------
void t_drag_artifact_source::drag_accepted( t_window * target )
{
	m_holder->artifact_accepted( this, target );
}

// -------------------------------------------------------------------
// window which holds an artifact which can be dragged away
// -------------------------------------------------------------------
void t_drag_artifact_source::drag_event( t_mouse_event const& event )
{
	if (m_artifact.get_icon() == k_artifact_none)
		return;

	m_holder->begin_artifact_drag( this );
}

// -------------------------------------------------------------------
// window which holds an artifact which can be dragged away
// -------------------------------------------------------------------
void t_drag_artifact_source::update()
{
	m_holder->update_artifacts();
}

// -------------------------------------------------------------------
// dragging class for artifacts
// -------------------------------------------------------------------
t_drag_artifact::t_drag_artifact( t_artifact const& artifact, t_drag_artifact_source* source )
               : m_artifact( artifact ), m_source( source )
{
	t_bitmap_layer_ptr image = get_icon( artifact.get_icon() );
	t_window*          window;
	t_screen_rect      rect = image->get_rect();

	set_hot_spot( t_screen_point( rect.right / 2, rect.bottom / 2 ) );
	window = new t_bitmap_layer_cache_window( image, t_screen_point(0,0), this );
}

// -------------------------------------------------------------------
// dragging class for artifacts
// -------------------------------------------------------------------
t_drag_artifact::~t_drag_artifact()
{
	if (m_artifact.get_icon() != k_artifact_none)
		m_source->restore( m_artifact );
	m_source->update();
}

// -------------------------------------------------------------------
// dragging class for artifacts
// -------------------------------------------------------------------
void t_drag_artifact::accepted( t_window* window )
{
	m_artifact = t_artifact();
	m_source->drag_accepted( window );
}
